<template>
  <div id="repetition">
    <Splitpanes horizontal>
      <Pane :size="40">
        <el-table
            ref="repetitionTable"
            v-loading="loading"
            row-key="id"
            :data="repetitionList"
            highlight-current-row
            @row-click="repetitionRowClick"
            height="100%"
        >
          <el-table-column label="操作人" prop="repDoctorName" width="100" show-overflow-tooltip>
          </el-table-column>
          <el-table-column
              label="复查时间"
              prop="repDate"
              min-width="160"
              show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
              prop="repReason"
              label="复查原因"
              min-width="160"
          >
            <template #default="scope">
              <el-input
                  v-model="scope.row.repReason"
                  @keyup.native.enter="saveRepReason(repetitionList)"
              ></el-input>
            </template>
          </el-table-column>
          <el-table-column label="是否采信" width="80" align="center">
            <template #default="scope">
              <el-tag :type="scope.row.isAccepted ===1  ? 'success' : 'danger'">
                {{ scope.row.isAccepted === 1 ? "是" : "否" }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="采信人" prop="accepterDoctorName" width="100" show-overflow-tooltip>
          </el-table-column>
          <el-table-column
              label="采信时间"
              prop="acceptDate"
              min-width="160"
              show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="80" align="center">
            <template #default="scope">
              <el-button
                  type="primary"
                  icon="el-icon-star-off"
                  :plain="!scope.row.isAccepted === 1"
                  @click="isAcceptedChange(scope.row)"
              >采信
              </el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </Pane>
      <Pane>
        <el-table
            ref="detailTable"
            v-loading="Dloading"
            row-key="id"
            :data="detailList"
            height="100%"
        >
          <el-table-column label="项目编号" prop="itemNo" width="120" show-overflow-tooltip>
          </el-table-column>
          <el-table-column label="项目名称" prop="itemNa" min-width="160" show-overflow-tooltip>
          </el-table-column>
          <el-table-column prop="result"  label="结果" min-width="160">
            <template #default="scope">
              <el-input
                  v-model="scope.row.result"
                  @keyup.native.enter="save(detailList)"
              ></el-input>
            </template>
          </el-table-column>
          <el-table-column
              prop="originalResult"
              label="原始结果"
          ></el-table-column>
        </el-table>
      </Pane>
    </Splitpanes>
  </div>
</template>
<script setup lang="ts">
import {getCurrentInstance, reactive, ref} from 'vue';
import {Pane, Splitpanes} from 'splitpanes';
import {VxeMessage, VxeNotifyType} from '@/utils/CommonMessage';
import {IsEmpty, DateToString} from '@/utils/common';
import {AppStore} from '@/stores/store';

let store = AppStore();

let {proxy}: any = getCurrentInstance();
let wlId = ref(''); //接收传过来的检验结果id
let loading = ref(false);
let Dloading = ref(false);
let repetitionList = reactive([]);
let detailList = reactive([]);
let currentRepetitionRow = reactive({});
let repetitionForm = reactive({
  id: null,
  maininfoId: null,
  repDate: null,//复查时间
  repDoctor: null,
  repReason: '',//复查原因
  isAccepted: false,//是否采信
  acceptDate: DateToString(new Date()),
  accepterDoctor: store.userInfo.id,
  accepterDoctorName: store.userInfo.realname
});

defineExpose({
  maininfoIdChange
})

function maininfoIdChange(maininfoId: any) {
  repetitionForm.maininfoId = maininfoId;
  proxy.$nextTick(() => {
    findRepetitionList(repetitionForm.maininfoId);
  });
}

function findRepetitionList(id) {
  repetitionList.length=0;
  detailList.length=0;
  if (IsEmpty(id)) {
    return;
  }
  loading.value = true;
  const url = '/lab/repetition/getRepetitionList/'+id;
  proxy.$http
      .get(url)
      .then((res: any) => {
        repetitionList.push(...res.data);
        if (repetitionList.length > 0) {
          repetitionRowClick(repetitionList[0]);
        } else {
          detailList.length=0;
        }
        loading.value = false;
      })
      .catch((err) => {
        loading.value = false;
      });
}

// 单击事件
function repetitionRowClick(row) {
  Dloading.value = true;
  detailList.length=0;
  (proxy.$refs.repetitionTable as any).setCurrentRow(row);
  detailList.push(...row.repetitionDetails);
  currentRepetitionRow = row;

  repetitionForm.id = row.id;
  repetitionForm.maininfoId = row.maininfoId;
  repetitionForm.repDate = row.repDate;//复查时间
  repetitionForm.repDoctor = row.repDoctor;
  repetitionForm.repReason = row.repReason;//复查原因
  Dloading.value = false;
}

function save(detailList) {
  if (IsEmpty(repetitionForm.id)) {
    VxeMessage('请选择一条复查记录', VxeNotifyType.error);
    return;
  }
  const details = [];
  if (detailList.length > 0) {
    detailList.forEach((ele) => {
      if (!IsEmpty(ele.result)) {
        details.push({
          id: ele.id,
          resultId: ele.resultId,
          itemNo: ele.itemNo,
          originalResult: ele.originalResult,
          result: ele.result,
        })
      }
    });
  }
  const url = '/lab/repetition/updateRepetitionDetail';
  proxy.$http
      .post(url, details)
      .then((res: any) => {
      })
      .catch((err) => {
        VxeMessage(err.data.error.message, VxeNotifyType.error);
      });
}
function saveRepReason(repetitionList) {

  const details = [];
  if (repetitionList.length > 0) {
    repetitionList.forEach((ele) => {
      if (!IsEmpty(ele.repReason)) {
        details.push({
          id: ele.id,
          repReason: ele.repReason,
        })
      }
    });
  }
  const url = '/lab/repetition/updateRepetition';
  proxy.$http
      .post(url, details)
      .then((res: any) => {
      })
      .catch((err) => {
        VxeMessage(err.data.error.message, VxeNotifyType.error);
      });
}
// 采信状态改变
function isAcceptedChange(row)
{
  if (row.isAccepted === 1) {
    VxeMessage('已采信，不可取消', VxeNotifyType.error);
    return;
  }
  //proxy.$emit('setResultByRepetition', row.details);

  const url = '/lab/repetition/setAccepted';
  const data = {
    id:row.id,
    acceptDate: repetitionForm.acceptDate,
    accepterDoctor: repetitionForm.accepterDoctor,
    accepterDoctorName: repetitionForm.accepterDoctorName,
  };
  proxy.$http
      .post(url, data)
      .then((res: any) => {
        repetitionForm.id = res.data.id;
        const idx = repetitionList.findIndex((item) => {
          return item.id === res.data.id;
        });
        repetitionList[idx].isAccepted = res.data.isAccepted;
        repetitionList[idx].accepterDoctor = res.data.accepterDoctor;
        repetitionList[idx].accepterDoctorName = res.data.accepterDoctorName;
        repetitionList[idx].acceptDate = res.data.acceptDate;
      })
      .catch((err) => {
        VxeMessage(err.status.msg, VxeNotifyType.error);
      });
}

</script>
<style lang="scss" scoped>
#repetition {
  height: calc(100% - 10px);
  margin: 5px;

  :deep(.el-table) {
    th,
    td {
      padding: 4px;
    }

    .el-button--primary {
      padding: 7px;
    }
  }
}
</style>